package br.com.netomarin.bundesliga.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.netomarin.bundesliga.model.MatchModel;
import br.com.netomarin.bundesliga.persistence.Match;

public class NotifyMatchServlet extends HttpServlet {

	private static final long serialVersionUID = 7087129216741901280L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String notifier = req.getParameter("notifier");
		String homeTeam = req.getParameter("home_team");
		String homeTeamP1 = req.getParameter("home_team_p1");
		String homeTeamP2 = req.getParameter("home_team_p2");
		byte scoreHomeTeam = Byte.parseByte(req.getParameter("score_home_team"));
		String visitorTeam = req.getParameter("visitor_team");
		String visitorTeamP1 = req.getParameter("visitor_team_p1");
		String visitorTeamP2 = req.getParameter("visitor_team_p2");
		byte scoreVisitorTeam = Byte.parseByte(req.getParameter("score_visitor_team"));
		String matchType = req.getParameter("match_type");
		
		Match m = new Match();
		m.setHomeTeam(homeTeam);
		m.setHomeP1(homeTeamP1);
		m.setHomeP2(homeTeamP2);
		m.setScoreHomeTeam(scoreHomeTeam);
		m.setVisitorTeam(visitorTeam);
		m.setVisitorP1(visitorTeamP1);
		m.setVisitorP2(visitorTeamP2);
		m.setScoreVisitorTeam(scoreVisitorTeam);
		m.setMatchType(matchType);
		m.setConfirmed(false);
		m.setNotifier(notifier);
		
		PrintWriter writer = resp.getWriter();
		if (MatchModel.notifyMatch(m)) {
			writer.write("Obrigado! Partida notificada com sucesso!");
		} else {
			writer.write("Ops... Erro ao notificar a partida. Tenta novamente ou procure o CTO da Bundesliga em netomarin@google.com");
		}
	}
}